package com.wyzz.hr.bo.impl;

import java.util.List;
import org.hibernate.criterion.*;

import com.wyzz.hr.bo.IAttdMachineBO;
import com.wyzz.hr.bo.IAttdMachineDAO;
import com.wyzz.hr.pojo.AttdMachine;

public class AttdMachineBOImpl implements IAttdMachineBO {

	public AttdMachineBOImpl() {
	}

	public List FindAllAttdMachine() {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.AttdMachine.class);
		dc.addOrder(Order.asc("macSortId"));
		return attdMachineDAO.findByCriteria(dc);
	}

	public List FindEnabledAttdMachine() {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.AttdMachine.class);
		dc.add(Restrictions.eq(AttdMachine.PROP_MAC_STATUS, new Integer(1)));
		dc.addOrder(Order.asc("macSortId"));
		return attdMachineDAO.findByCriteria(dc);
	}

	public boolean addAttdMachine(AttdMachine attdMachine) {
		String trimmedAttdMachineNo = attdMachine.getMacNo().trim();
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.AttdMachine.class);
		dc.add(Restrictions.eq(AttdMachine.PROP_MAC_NO, trimmedAttdMachineNo));
		List oldMachine = attdMachineDAO.findByCriteria(dc);
		if (oldMachine.isEmpty()) {
			attdMachine.setMacNo(trimmedAttdMachineNo);
			attdMachineDAO.saveObject(attdMachine);
			return true;
		}
		try {
			return false;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public String delAttdMachine(Class name, String id) {
		StringBuffer msg = new StringBuffer();
		try {
			attdMachineDAO.deleteObject(com.wyzz.hr.pojo.AttdMachine.class, id);
		} catch (Exception e) {
			msg.append((new StringBuilder()).append(
					"\u7CFB\u7EDF\u5F02\u5E38\uFF1A").append(e.getMessage())
					.toString());
		}
		return msg.toString();
	}

	public AttdMachine loadAttdMachine(String macId) {
		Object ob = attdMachineDAO
				.loadObject(com.wyzz.hr.pojo.AttdMachine.class, macId, null,
						new boolean[0]);
		if (ob == null)
			return null;
		else
			return (AttdMachine) ob;
	}

	public List findAttdMachineByIds(String macIds[]) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.AttdMachine.class);
		dc.add(Restrictions.in(AttdMachine.PROP_MAC_ID, macIds));
		List result = attdMachineDAO.findByCriteria(dc);
		if (result.isEmpty())
			return null;
		else
			return result;
	}

	public void saveAttdMachineSortIdByAttdMachine(String ids[]) {
		int length = ids.length;
		int sortId = 1;
		for (int i = 0; i < length; i++) {
			attdMachineDAO.exeHql((new StringBuilder()).append(
					"update AttdMachine set macSortId=").append(sortId).append(
					" where id ='").append(ids[i]).append("'").toString());
			sortId++;
		}

	}

	public String updateAttdMachine(AttdMachine attdMachine) {
		String trimmedAttdMachineMacNo = attdMachine.getMacNo().trim();
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.AttdMachine.class);
		dc
				.add(Restrictions.ne(AttdMachine.PROP_MAC_ID, attdMachine
						.getMacId()));
		dc.add(Restrictions
				.eq(AttdMachine.PROP_MAC_NO, trimmedAttdMachineMacNo));
		List oldMachine = attdMachineDAO.findByCriteria(dc);
		if (oldMachine.isEmpty()) {
			AttdMachine oldAttdMachine = (AttdMachine) attdMachineDAO
					.loadObject(com.wyzz.hr.pojo.AttdMachine.class, attdMachine
							.getMacId().trim(), null, new boolean[0]);
			oldAttdMachine.setMacNo(attdMachine.getMacNo());
			oldAttdMachine.setMacDesc(attdMachine.getMacDesc());
			oldAttdMachine.setMacIP(attdMachine.getMacIP());
			oldAttdMachine.setMacPort(attdMachine.getMacPort());
			oldAttdMachine.setMacPassword(attdMachine.getMacPassword());
			oldAttdMachine.setMacHasFinger(attdMachine.getMacHasFinger());
			oldAttdMachine.setMacStatus(attdMachine.getMacStatus());
			oldAttdMachine.setMacClass(attdMachine.getMacClass());
			attdMachineDAO.updateObject(oldAttdMachine);
			return "0";
		} else {
			return "2";
		}
	}

	public IAttdMachineDAO getAttdMachineDAO() {
		return attdMachineDAO;
	}

	public void setAttdMachineDAO(IAttdMachineDAO attdMachineDAO) {
		this.attdMachineDAO = attdMachineDAO;
	}

	private IAttdMachineDAO attdMachineDAO;
}
